package com.sk.photo_album;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;

import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.IntRange;
import androidx.core.content.FileProvider;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class AlbumBuilder implements Serializable {

    /**
     * 媒体文件类型
     */
    private Media mediaType = Media.MEDIA_TYPE_ALL;

    /**
     * 图片文件类型查询语句
     */
    private String imageSelection = null;

    /**
     * 图片文件类型
     */
    private String[] imageSelectionArgs = null;

    /**
     * 视频文件类型查询语句
     */
    private String videoSelection = null;

    /**
     * 视频文件类型
     */
    private String[] videoSelectionArgs = null;

    /**
     * 单次可选数量 默认 9 张
     */
    private int maxSelect = 9;

    /**
     * 是否需要相机
     */
    private Boolean showCamera = true;

    /**
     * FileProvider 使用
     */
    private String authority = "com.sk.fileprovider";

    /**
     * 是否裁剪
     */
    private Boolean cropImage = false;

    /**
     * 拍照后存储位置
     */
    private String savePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();

    private transient ActivityResultLauncher<Intent> mActivityResultLauncher;

    /**
     * 监听返回结果
     */
    private transient AlbumCallBack callBack;

    private static AlbumBuilder instance;

    private AlbumBuilder() {}

    public static AlbumBuilder getInstance() {
        if (instance == null) {
            synchronized (AlbumBuilder.class) {
                if (instance == null) {
                    instance = new AlbumBuilder();
                }
            }
        }

        return instance;
    }

    /**
     * 启动页面
     *
     * @param context context
     */
    public void start(Context context) {
        context.startActivity(new Intent(context, AlbumActivity.class));
    }

    /**
     * 设置需要显示的媒体类型
     *
     * @param type 类型
     */
    public AlbumBuilder setMediaType(Media type) {
        this.mediaType = type;
        return this;
    }

    /**
     * @param typeArgs 图片类型列表
     */
    private AlbumBuilder setImageSelectionArgs(Media.IMAGE_TYPE... typeArgs) {
        imageSelectionArgs = new String[typeArgs.length];
        // 请求语句组装
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < typeArgs.length; i++) {
            imageSelectionArgs[i] = typeArgs[i].getType();
            if (builder.length() != 0) {
                builder.append("or ");
            }
            builder.append(MediaStore.Images.Media.MIME_TYPE);
            builder.append("=? ");
        }

        this.imageSelection = builder.toString();

        return this;
    }

    /**
     * @param typeArgs 视频类型列表
     */
    private AlbumBuilder setVideoSelectionArgs(Media.VIDEO_TYPE... typeArgs) {
        videoSelectionArgs = new String[typeArgs.length];
        // 请求语句组装
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < typeArgs.length; i++) {
            videoSelectionArgs[i] = typeArgs[i].getType();
            if (builder.length() != 0) {
                builder.append("or ");
            }
            builder.append(MediaStore.Video.Media.MIME_TYPE);
            builder.append("=? ");
        }

        this.videoSelection = builder.toString();

        return this;
    }

    /**
     * 最大可选图片数量（9>=max>0）
     *
     * @param maxSelect 最大选择数量
     */
    public AlbumBuilder setMaxSelect(@IntRange(from = 1, to = 9) int maxSelect) {
        this.maxSelect = maxSelect;
        return this;
    }

    public Boolean getShowCamera() {
        return showCamera;
    }

    /**
     * 是否显示相机按钮
     *
     * @param need true false
     */
    public AlbumBuilder setShowCamera(Boolean need) {
        this.showCamera = need;
        return this;
    }

    public String getAuthority() {
        return authority;
    }

    public AlbumBuilder setAuthority(String authority) {
        this.authority = authority;
        return this;
    }

    public Boolean getCropImage() {
        return cropImage;
    }

    private AlbumBuilder setCropImage(Boolean cropImage) {
        this.cropImage = cropImage;
        return this;
    }

    public String getSavePath() {
        return savePath;
    }

    /**
     * 设置图片存储位置
     *
     * @param savePath 图片存储地址
     */
    public AlbumBuilder setSavePath(String savePath) {
        this.savePath = savePath;
        return this;
    }

    public Media getMediaType() {
        return mediaType;
    }

    public String getImageSelection() {
        return imageSelection;
    }

    public String[] getImageSelectionArgs() {
        return imageSelectionArgs;
    }

    public String getVideoSelection() {
        return videoSelection;
    }

    public String[] getVideoSelectionArgs() {
        return videoSelectionArgs;
    }

    public int getMaxSelect() {
        return maxSelect;
    }

    public AlbumCallBack getCallBack() {
        return callBack;
    }

    public AlbumBuilder setCallBack(AlbumCallBack callBack) {
        this.callBack = callBack;
        return this;
    }
}
